from django.utils import timezone
from rest_framework import serializers
from .models import MealPlan, DailyMeal, FoodItem,NutritionAnalysis
from user.models import UserProfile


class FoodItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = FoodItem
        fields = ['id', 'name', 'category', 'calories', 'protein', 'carbs', 'fat', 'fiber', 'description']


class DailyMealSerializer(serializers.ModelSerializer):
    breakfast = FoodItemSerializer(many=True, read_only=True)
    lunch = FoodItemSerializer(many=True, read_only=True)
    dinner = FoodItemSerializer(many=True, read_only=True)
    snacks = FoodItemSerializer(many=True, read_only=True)

    class Meta:
        model = DailyMeal
        fields = ['date', 'breakfast', 'lunch', 'dinner', 'snacks']





class MealPlanSerializer(serializers.ModelSerializer):
    daily_meals = DailyMealSerializer(many=True, read_only=True, source='daily_meals.all')

    class Meta:
        model = MealPlan
        fields = ['id', 'created_at', 'start_date', 'end_date', 'total_calories', 'strategy_used', 'daily_meals']

class UserProfileSerializer(serializers.ModelSerializer):
    age = serializers.ReadOnlyField()
    bmi = serializers.ReadOnlyField()
    bmi_category = serializers.ReadOnlyField()

    class Meta:
        model = UserProfile
        fields = [
            'id', 'nickname', 'avatar', 'gender', 'birth_date', 'height', 'weight',
            'fitness_goal', 'fitness_level', 'dietary_preference', 'activity_level',
            'has_diabetes', 'has_hypertension', 'has_heart_disease', 'food_allergies',
            'dietary_restrictions', 'preferred_workout_duration', 'preferred_workout_times',
            'age', 'bmi', 'bmi_category'
        ]


class NutritionAnalysisSerializer(serializers.ModelSerializer):
    user_profile = UserProfileSerializer(read_only=True, source='user.profile')

    class Meta:
        model = NutritionAnalysis
        fields = '__all__'
        read_only_fields = ('nutrition_text', 'advice', 'created_at')



